import java.util.concurrent.TimeUnit;

/**
 * @author wcj
 * @Date 2021/6/8 11:53
 * @Version 1.0
 */
public class Join {

    /**
     * join（）:即调用线程终止之后才会返回当前线程，内部也是典型的灯带通知机制
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        Thread previous = Thread.currentThread();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Domino(previous), "Thread-" + i);
            thread.start();
            previous = thread;
        }
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getName() + "terminated");
    }

    static class Domino implements Runnable {
        private Thread t;

        public Domino(Thread t) {
            this.t = t;
        }

        @Override
        public void run() {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " terminated.");
        }
    }
}
